package com.dragon.mmochat.model.task;

import java.awt.Color;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.message.Mmochat_1_7S;
import com.dragon.mmochat.message.Mmochat_5_10S;
import com.dragon.mmochat.model.MmochatDailyActivity;
import com.dragon.mmochat.model.award.MmochatEquipAward;
import com.dragon.mmochat.model.enumType.MmochatDailyActivityType;
import com.dragon.mmochat.model.enumType.MmochatMaterialType;
import com.dragon.mmochat.model.enumType.MmochatPetStateType;
import com.dragon.mmochat.model.enumType.MmochatPropType;
import com.dragon.mmochat.model.enumType.MmochatRoleAttributeType;
import com.dragon.mmochat.model.enumType.MmochatRoleType;
import com.dragon.mmochat.model.enumType.MmochatSmallMoneyCostType;
import com.dragon.mmochat.model.enumType.MmochatSmallMoneyEarnType;
import com.dragon.mmochat.model.enumType.MmochatTaskStateType;
import com.dragon.mmochat.model.enumType.MmochatTaskType;
import com.dragon.mmochat.model.gang.MmochatGang;
import com.dragon.mmochat.model.gang.MmochatGangMember;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.model.map.MmochatMapPoint;
import com.dragon.mmochat.model.object.MmochatDrug;
import com.dragon.mmochat.model.object.MmochatEquip;
import com.dragon.mmochat.model.object.MmochatMaterial;
import com.dragon.mmochat.model.object.MmochatProp;
import com.dragon.mmochat.model.role.MmochatMonster;
import com.dragon.mmochat.model.role.MmochatNpc;
import com.dragon.mmochat.model.role.MmochatPet;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.service.MmochatBattleService;
import com.dragon.mmochat.service.MmochatChatService;
import com.dragon.mmochat.service.MmochatDao;
import com.dragon.mmochat.service.MmochatEquipService;
import com.dragon.mmochat.service.MmochatGangService;
import com.dragon.mmochat.service.MmochatMapService;
import com.dragon.mmochat.service.MmochatNpcService;
import com.dragon.mmochat.service.MmochatPetService;
import com.dragon.mmochat.service.MmochatPropService;
import com.dragon.mmochat.service.MmochatStatisticsService;
import com.dragon.mmochat.service.MmochatTaskService;
import com.dragon.mmochat.util.MmochatUtil;

public class MmochatTask implements Serializable {
	private static final long serialVersionUID = -5658351733446869076L;
	private int taskId = 0; // 任务ID
	private String taskName; // 任务标题
	private MmochatTaskType taskType = MmochatTaskType.其它; // 任务类型
	private MmochatTaskStateType taskState = MmochatTaskStateType.未接; // 任务进度
	private boolean showInTaskList = true; // 是否显示在可接任务列表

	// 循环任务的轮次
	private int taskTurn = 1;
	private int taskMaxTurn = 10;

	// 接任务条件
	private int accept_npcId = Integer.MIN_VALUE; // 接任务NPC
	private int accept_minLevel = 0; // 最低等级要求
	private int accept_maxLevel = 1000; // 最大等级要求
	private List<Integer> accept_preTaskIds = new ArrayList<Integer>(); // 前置任务id

	// 接任务对话
	private List<MmochatTaskTalk> accept_talk = new ArrayList<MmochatTaskTalk>(); // 接任务对话
	private int cur_accept_talk_index = 0; // 当前接任务对话进行到第几句
	private long accept_time = 0; // 接任务时间
	private boolean canReject = true; // 是否可以不接此任务

	// 接任务对话完成后的闪动提示
	private Integer hintKey = null; // 闪动的按键
	private String hintWord = null; // 闪动的提示语

	// 完成任务条件
	private int finish_npcId = Integer.MIN_VALUE; // 交任务NPC
	private List<MmochatKillMonster> killList = new ArrayList<MmochatKillMonster>(); // 杀怪列表
	private List<MmochatMaterial> finish_need_materials = new ArrayList<MmochatMaterial>(); // 完成任务所需的材料
	private List<MmochatDrug> finish_need_drugs = new ArrayList<MmochatDrug>(); // 完成任务所需的药品
	private int finish_need_money = 0; // 完成任务所需的金钱
	private int finish_need_zhanji = 0; // 完成任务所需的战绩

	// 任务介绍
	private String taskDetail = null;

	// 交任务对话
	private List<MmochatTaskTalk> finish_talk = new ArrayList<MmochatTaskTalk>(); // 交任务对话
	private int cur_finish_talk_index = 0; // 当前接任务对话进行到第几句
	private long finish_time = 0; // 交任务时间

	// 交任务时需要完成的NPC战斗
	private MmochatNpcBattle finish_need_battles = null;

	// 任务奖励
	private int finish_award_exp = 0; // 经验奖励
	private int finish_award_expTimes = 0; // 经验奖励倍数(相对于常规打怪)
	private int finish_award_dao = 0; // 道行奖励
	private int finish_award_money = 0; // 完成任务奖励的金钱
	private int finish_award_gangBuild = 0; // 完成任务奖励的帮派贡献
	private int finish_award_gangMoney = 0; // 完成任务奖励的帮派资金
	private List<MmochatMaterial> finish_award_materials = new ArrayList<MmochatMaterial>(); // 完成任务奖励的材料
	private List<MmochatDrug> finish_award_drugs = new ArrayList<MmochatDrug>(); // 完成任务奖励的药品
	private List<MmochatEquipAward> finish_award_equips = new ArrayList<MmochatEquipAward>(); // 完成任务奖励的装备
	private List<MmochatMonster> finish_award_pet = new ArrayList<MmochatMonster>(); // 宠物奖励
	private List<MmochatPropType> finish_award_prop = new ArrayList<MmochatPropType>(); // 道具奖励

	// 任务指引
	private Integer next_taskId = null; // 下一个任务ID
	private String next_taskHintName = null; // 任务指引的任务名
	private String next_taskHint = null; // 任务指引内容
	// 下一个任务的NPCID,如果非null，则显示此NPC的行走路线，用于新手向导
	// 在此任务交任务对话完成时显示路线
	private Integer guide_next_taskNpcId = null;

	// 连接新任务
	private Integer nextAccept_taskId = null; // 任务完成后自动接起的新任务

	// 待完成
	private boolean taskContinue = false; // 是否在任务完成后设置为<待完成>而不是<完成>

	// 同时完成另一个任务
	private int finishAnotherTaskId = -1; // 完成本任务的同时完成另一个任务

	// 任务寿命
	private long taskLife = 0; // 任务时间,ms

	// 任务数据
	private int targetMapId; // 目标地图
	private int targetX, targetY;// 目标坐标
	private int taskLevel; // 任务等级
	private int taskTag; // 任务标志

	public MmochatTask getClone() {
		return (MmochatTask) MmochatUtil.deepClone(this);
	}

	// 获取任务自动寻路点
	public MmochatMapPoint getGuiderPoint(MmochatPlayer me) {
		return getGuiderPoint(me, taskState);
	}

	public MmochatMapPoint getGuiderPoint(MmochatPlayer me,
			MmochatTaskStateType theTaskState) {
		switch (theTaskState) {
		case 已接: {
			if (getTaskType() != MmochatTaskType.过渡任务
					&& getTaskType() != MmochatTaskType.寻宝任务
					&& getTaskType() != MmochatTaskType.真假情报) {
				// 需要杀怪，引向杀怪地图
				for (MmochatKillMonster kill : getKillList()) {
					if (kill.getCurKillMonsterNum() < kill.getMonsterNum()) {
						for (MmochatMap m : MmochatMapService.maps.values()) {
							if (m.hasTheMonster(kill.getMonsterType())) {
								MmochatMapPoint p = new MmochatMapPoint();
								p.setMapId(m.getMapId());
								p.setX(150);
								p.setY(150);
								return p;
							}
						}
					}
				}
				// 需要收集材料，引向材料掉落地图
				for (MmochatMaterial material : getFinish_need_materials()) {
					if (material.getNum() > 0) {
						int myNum = me.getPackageObjNum(material.getType());
						if (myNum < material.getNum()) {
							for (MmochatMap m : MmochatMapService.maps.values()) {
								if (m.hasTheMonsterMaterial(material.getType())) {
									MmochatMapPoint p = new MmochatMapPoint();
									p.setMapId(m.getMapId());
									p.setX(150);
									p.setY(150);
									return p;
								}
							}
						}
					}
				}

				// 引向交任务NPC
				MmochatNpc npc = MmochatNpcService
						.getNpcById(getFinish_npcId());
				if (npc != null) {
					MmochatMapPoint p = new MmochatMapPoint();
					p.setMapId(npc.getMapId());
					p.setX(npc.getX());
					p.setY(npc.getY());
					return p;
				}
			} else if (getTaskType() == MmochatTaskType.过渡任务) {
				// 引向交任务NPC
				MmochatNpc npc = MmochatNpcService
						.getNpcById(getFinish_npcId());
				if (npc != null) {
					MmochatMapPoint p = new MmochatMapPoint();
					p.setMapId(npc.getMapId());
					p.setX(npc.getX());
					p.setY(npc.getY());
					return p;
				}
			} else if (getTaskType() == MmochatTaskType.寻宝任务
					|| getTaskType() == MmochatTaskType.真假情报) {
				// 引向寻宝点
				MmochatMapPoint p = new MmochatMapPoint();
				p.setMapId(targetMapId);
				p.setX(targetX);
				p.setY(targetY);
				return p;
			}
			break;
		}
		case 未接: {
			// 引向接任务NPC
			MmochatNpc npc = MmochatNpcService.getNpcById(getAccept_npcId());
			if (npc != null) {
				MmochatMapPoint p = new MmochatMapPoint();
				p.setMapId(npc.getMapId());
				p.setX(npc.getX());
				p.setY(npc.getY());
				return p;
			}
			break;
		}
		default:
			break;
		}
		return null;
	}

	// 玩家是否满足条件接任务
	public boolean canAccept(MmochatPlayer p) {
		// TODO
		MmochatTask task = MmochatTaskService.getTaskById(taskId);
		if (task == null) {
			task = this;
		}

		// 等级要求
		if (p.getLevel() < task.getAccept_minLevel()) {
			return false;
		}
		if (p.getLevel() > task.getAccept_maxLevel()) {
			return false;
		}
		// 前置任务要求
		for (Integer taskId : task.getAccept_preTaskIds()) {
			if (!p.hasFinished(taskId)) {
				return false;
			}
		}
		MmochatTask t = p.getTaskByType(task.getTaskType());
		if (t != null && t.getTaskType().isLoopTask()) {
			if (t.getTaskState() == MmochatTaskStateType.已接
					|| t.getTaskState() == MmochatTaskStateType.待完成) {
				return false;
			} else if (t.getTaskState() == MmochatTaskStateType.已完成) {
				p.getTasks().remove(t.getTaskId());
			}
		}
		return true;
	}

	// 玩家p是否满足条件完成任务
	public boolean canFinish(MmochatPlayer p) {
		// TODO
		// 杀怪数量
		for (MmochatKillMonster killTask : killList) {
			if (killTask.getMonsterType() != null
					&& killTask.getMonsterNum() > 0) {
				if (killTask.getCurKillMonsterNum() < killTask.getMonsterNum()) {
					return false;
				}
			}
		}

		// 收集材料
		for (MmochatMaterial material : finish_need_materials) {
			if (material.getNum() > 0) {
				if (p.getPackageObjNum(material.getMaterialType()) < material
						.getNum()) {
					return false;
				}
			}
		}

		// 收集药品
		for (MmochatDrug drug : finish_need_drugs) {
			if (drug.getNum() > 0) {
				if (p.getPackageObjNum(drug.getDrugType()) < drug.getNum()) {
					return false;
				}
			}
		}

		// 金钱
		if (p.getSmall_money() < finish_need_money) {
			return false;
		}

		// 战绩
		if (p.getZhanji() < finish_need_zhanji) {
			return false;
		}
		return true;
	}

	// 完成任务，在调用前应该作预判断，是否能完成任务，否则会有问题
	@SuppressWarnings("deprecation")
	public boolean finishTask(MmochatPlayer me) {
		String costInfo = "";

		// 完成任务所需的材料
		for (MmochatMaterial material : finish_need_materials) {
			if (material.getNum() > 0) {
				if (me.getPackageObjNum(material.getType()) < material.getNum()) {
					return false;
				}
				me.delPackageObj(material.getType(), material.getNum());
				costInfo += material.getNum() + "个"
						+ material.getType().toString() + ",";
			}
		}

		// 完成任务所需的药品
		for (MmochatDrug drug : finish_need_drugs) {
			if (drug.getNum() > 0) {
				if (me.getPackageObjNum(drug.getType()) < drug.getNum()) {
					return false;
				}
				me.delPackageObj(drug.getType(), drug.getNum());
				costInfo += drug.getNum() + "个" + drug.getType().toString()
						+ ",";
			}
		}

		// 完成任务所需的金钱
		if (finish_need_money > 0) {
			if (me.getSmall_money() < finish_need_money) {
				return false;
			}
			me.addAndUpdateSmall_money(-1 * finish_need_money);
			MmochatStatisticsService.addSmallMoneyCost(
					MmochatSmallMoneyCostType.任务消耗, -1 * finish_need_money);
			costInfo += MmochatUtil.getColorMoney(finish_need_money) + "文钱,";
		}

		// 完成任务所需的战绩
		if (finish_need_zhanji > 0) {
			if (me.getZhanji() < finish_need_zhanji) {
				return false;
			}
			me.addZhanji(-1 * finish_need_zhanji);
			costInfo += finish_need_zhanji + "点战绩,";
		}

		// TODO:发放任务奖励

		String myEarnInfo = "";
		String myPetEarnInfo = "";
		String lostEarnInfo = "";
		String uplevelInfo = "";
		MmochatPet pkPet = me.getPkPet();
		// 经验奖励
		boolean uplevel = false;
		boolean petUplevel = false;
		if (finish_award_exp > 0) {
			uplevel = me.addExp(finish_award_exp);
			myEarnInfo += finish_award_exp + "点经验,";
			if (pkPet != null) {
				int petExp = (int) (MmochatConstant.petExpPercentOfPerson * finish_award_exp);
				petExp = MmochatBattleService.getRealExp(petExp, pkPet, me);
				petUplevel = pkPet.addExp(me, petExp);
				myPetEarnInfo += petExp + "点经验,";
			}
		}

		// 经验倍数奖励
		boolean uplevel2 = false;
		boolean petUplevel2 = false;
		if (finish_award_expTimes > 0) {
			Map<MmochatRoleAttributeType, Double> attrMap = MmochatPetService.monsterBaseAttackData
					.get(me.getTrueLevel());
			if (attrMap != null) {
				int exp = (int) (attrMap.get(MmochatRoleAttributeType.杀死怪物经验) * finish_award_expTimes);
				uplevel2 = me.addExp(exp);
				myEarnInfo += exp + "点经验,";
				if (pkPet != null) {
					int petExp = (int) (MmochatConstant.petExpPercentOfPerson * exp);
					petExp = MmochatBattleService.getRealExp(petExp, pkPet, me);
					petUplevel2 = pkPet.addExp(me, petExp);
					myPetEarnInfo += petExp + "点经验,";
				}
			}
		}

		// 道行奖励
		if (finish_award_dao > 0) {
			// 刷道比赛
			MmochatPropService.dealDaoActivity(me, finish_award_dao);

			int realDao = MmochatBattleService.getRealDao(me, finish_award_dao);
			me.addDao(realDao);
			myEarnInfo += realDao + "天道行(削减前" + finish_award_dao + "天),";
			if (pkPet != null) {
				realDao = MmochatBattleService.getRealDao(pkPet,
						finish_award_dao);
				pkPet.addDao(realDao);
				myPetEarnInfo += realDao + "点武学,";
			}
		}

		// 赠品道具奖励
		for (MmochatPropType propType : finish_award_prop) {
			if (propType != null) {
				MmochatProp newProp = new MmochatProp(propType);
				newProp.setCanDeal(false);
				boolean ret = me.addObjectToPackage(newProp);
				if (ret) {
					myEarnInfo += "一个"
							+ MmochatUtil.wrapColor(newProp.getName(),
									Color.yellow) + ",";
				} else {
					lostEarnInfo += "一个"
							+ MmochatUtil.wrapColor(newProp.getName(),
									Color.yellow) + "。";
				}
			}
		}

		// 金钱奖励
		if (finish_award_money > 0) {
			me.addAndUpdateSmall_money(finish_award_money);
			MmochatStatisticsService.addSmallMoneyEarn(
					MmochatSmallMoneyEarnType.任务产出, finish_award_money);
			myEarnInfo += MmochatUtil.getColorMoney(finish_award_money) + "文钱,";
		}

		boolean needUpdateGang = false;
		MmochatGang gang = null;
		// 帮贡奖励
		int totalGangBuildAward = 0;
		if (finish_award_gangBuild > 0) {
			if (me.hasGang()) {
				gang = MmochatGangService.getGangById(me.getGangId());
				if (gang != null) {
					MmochatGangMember myInfo = gang.getMembers().get(
							me.getRoleId());
					if (myInfo != null) {
						myInfo.addBuildValue(finish_award_gangBuild);
						gang.addBuildValue(finish_award_gangBuild);
						needUpdateGang = true;
						myEarnInfo += finish_award_gangBuild + "点帮贡和帮派建设度,";
						totalGangBuildAward += finish_award_gangBuild;
					}
				}
			}
		}

		// 帮派资金奖励
		if (finish_award_gangMoney > 0) {
			if (me.hasGang()) {
				gang = MmochatGangService.getGangById(me.getGangId());
				if (gang != null) {
					gang.addMoney(finish_award_gangMoney);
					needUpdateGang = true;
				}
			}
		}

		// 如果是帮派任务，每天前10个将额外获得20点帮贡，365天道行，365点武学
		if (getTaskType() == MmochatTaskType.帮派任务) {
			boolean updateActivity = false;
			MmochatDailyActivity activity = me.getDailyActivities().get(
					MmochatDailyActivityType.帮派任务额外奖励);
			if (activity == null) {
				activity = new MmochatDailyActivity();
				activity.setType(MmochatDailyActivityType.帮派任务额外奖励);
				me.addDailyActivity(activity);
				updateActivity = true;
			}
			Date now = new Date();
			Date lastDate = new Date(activity.getLastTime());
			boolean hasExtraAward = false;
			if (now.getDate() == lastDate.getDate()
					&& now.getMonth() == lastDate.getMonth()
					&& now.getYear() == lastDate.getYear()) {
				// 今天已经做过帮派任务了
				if (activity.getDailyTimes() >= activity.getType()
						.getMaxDailyTimes()) {
					// 没有额外奖励
				} else {
					activity.addDailyTimes(1);
					hasExtraAward = true;
					updateActivity = true;
				}
			} else {
				// 今天第一次
				activity.setLastTime(now.getTime());
				activity.setDailyTimes(1);
				hasExtraAward = true;
				updateActivity = true;
			}
			if (hasExtraAward) {
				int gangTaskTurn = activity.getDailyTimes();
				int extraGangBuildAward = 10 + gangTaskTurn * 5;
				myEarnInfo += "今天第" + gangTaskTurn + "个任务额外奖励"
						+ extraGangBuildAward + "点帮贡和建设度，";
				// 道行武学奖励
				int extraDao = 300 + gangTaskTurn * 20;
				// 刷道比赛
				MmochatPropService.dealDaoActivity(me, extraDao);

				int realDao = MmochatBattleService.getRealDao(me, extraDao);
				me.addDao(realDao);
				myEarnInfo += realDao + "天道行(削减前" + extraDao + "天)，";
				if (pkPet != null) {
					realDao = MmochatBattleService.getRealDao(pkPet, extraDao);
					pkPet.addDao(realDao);
					myEarnInfo += realDao + "点武学，";
				}
				// 额外帮贡奖励
				if (me.hasGang()) {
					gang = MmochatGangService.getGangById(me.getGangId());
					if (gang != null) {
						MmochatGangMember myInfo = gang.getMembers().get(
								me.getRoleId());
						if (myInfo != null) {
							myInfo.addBuildValue(extraGangBuildAward);
							gang.addBuildValue(extraGangBuildAward);
							needUpdateGang = true;
							totalGangBuildAward += extraGangBuildAward;
						}
					}
				}
			}
			if (updateActivity) {
				try {
					MmochatDao.updateRoleDailyActivity(me);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		if (needUpdateGang) {
			try {
				MmochatDao.updateAfterGangTask(gang, totalGangBuildAward,
						finish_award_gangMoney);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		// 材料奖励
		for (MmochatMaterial material : finish_award_materials) {
			if (material.getNum() > 0) {
				boolean success = me.addMaterial(material.getType(), material
						.getNum());
				if (success) {
					myEarnInfo += material.getNum() + "个"
							+ material.getType().toString() + ",";
				} else {
					lostEarnInfo += material.getNum() + "个"
							+ material.getType().toString() + ",";
				}
			}
		}

		// 药品奖励
		for (MmochatDrug drug : finish_award_drugs) {
			if (drug.getNum() > 0) {
				boolean success = me.addDrug(drug.getType(), drug.getNum());
				if (success) {
					myEarnInfo += drug.getNum() + "个"
							+ drug.getType().toString() + ",";
				} else {
					lostEarnInfo += drug.getNum() + "个"
							+ drug.getType().toString() + ",";
				}
			}
		}

		// 装备奖励
		for (MmochatEquipAward equipAward : finish_award_equips) {
			if (MmochatUtil.isInPercent(equipAward.getOpportunity())) {
				MmochatEquip equip = MmochatEquipService.createRandomEquip(
						true, equipAward.getEquipType(), equipAward.getLevel(),
						equipAward.getQuality());
				if (equip == null) {
					continue;
				}
				equip.setEquipModifyLevel(equipAward.getModify());
				boolean success = me.addObjectToPackage(equip);
				if (success) {
					myEarnInfo += MmochatUtil.wrapColor(equip.getName(), equip
							.getQuality().getColor())
							+ ",";
				} else {
					lostEarnInfo += MmochatUtil.wrapColor(equip.getName(),
							equip.getQuality().getColor())
							+ ",";
				}
			}
		}

		// 宠物奖励
		for (MmochatMonster monster : finish_award_pet) {
			MmochatPet pet = MmochatPetService.createPetFromMonster(monster,
					true);
			if (pet != null) {
				boolean ret = me.addPet(pet);
				if (me.getPkPet() == null) {
					pet.setState(MmochatPetStateType.参战);
				}
				if (ret) {
					myEarnInfo += "一个宠物:" + pet.getName() + ",";
					Mmochat_5_10S pack = new Mmochat_5_10S();
					pack.addPet(pet);
					MmochatUtil.sendCommonPack(me, pack);
				} else {
					lostEarnInfo += "一个宠物:" + pet.getName() + ",";
				}
			}
		}

		if (uplevel || uplevel2) {
			uplevelInfo += "\n"
					+ MmochatUtil.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG)
					+ MmochatUtil.wrapColor("恭喜!您升到" + me.getLevel() + "级了!",
							Color.green);
		}
		if (pkPet != null && (petUplevel || petUplevel2)) {
			uplevelInfo += "\n"
					+ MmochatUtil.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG)
					+ MmochatUtil.wrapColor("恭喜!您的" + pkPet.getName() + "升到"
							+ pkPet.getLevel() + "级了!", Color.green);
		}

		String info = "";
		if (costInfo.length() > 0) {
			info += "您完成任务时花费了" + costInfo;
		}
		if (myEarnInfo.length() > 0) {
			info += "您获得了" + myEarnInfo;
		}
		if (myPetEarnInfo.length() > 0) {
			info += "您的参战宠物获得了" + myPetEarnInfo;
		}
		if (lostEarnInfo.length() > 0) {
			info += MmochatUtil.wrapColor("您的包裹已满，无法获得" + lostEarnInfo,
					Color.red)
					+ "请及时清理,";
		}
		if (info.length() > 0) {
			info += "任务完成!";
		}
		if (uplevelInfo.length() > 0) {
			info += uplevelInfo;
		}

		if (info.length() > 0) {
			MmochatChatService.sendSystemMsgToPerson(me, info);
		}

		if (taskContinue) {
			setTaskState(MmochatTaskStateType.待完成);
		} else {
			setTaskState(MmochatTaskStateType.已完成);
		}
		if (finishAnotherTaskId != -1) {
			MmochatTask anotherTask = me.getTaskById(finishAnotherTaskId);
			if (anotherTask != null) {
				anotherTask.setTaskState(MmochatTaskStateType.已完成);
			}
		}

		if (getTaskType().isLoopTask()) {
			if (getTaskState() == MmochatTaskStateType.已完成) {
				me.getTasks().remove(getTaskId());
			}
		}
		return true;
	}

	public int getTaskId() {
		return taskId;
	}

	public void setTaskId(int taskId) {
		this.taskId = taskId;
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public MmochatTaskStateType getTaskState() {
		return taskState;
	}

	public void setTaskState(MmochatTaskStateType taskState) {
		if (taskState == MmochatTaskStateType.已接) {
			accept_time = System.currentTimeMillis();
		} else if (taskState == MmochatTaskStateType.已完成) {
			finish_time = (new Date()).getTime();
		}
		this.taskState = taskState;
	}

	public int getAccept_minLevel() {
		return accept_minLevel;
	}

	public void setAccept_minLevel(int acceptMinLevel) {
		accept_minLevel = acceptMinLevel;
	}

	public List<Integer> getAccept_preTaskIds() {
		return accept_preTaskIds;
	}

	public void setAccept_preTaskIds(List<Integer> acceptPreTaskIds) {
		accept_preTaskIds = acceptPreTaskIds;
	}

	public void addAccept_preTaskId(int taskId) {
		accept_preTaskIds.add(taskId);
	}

	public int getAccept_npcId() {
		return accept_npcId;
	}

	public void setAccept_npcId(int acceptNpcId) {
		accept_npcId = acceptNpcId;
	}

	public int getFinish_npcId() {
		return finish_npcId;
	}

	public void setFinish_npcId(int finishNpcId) {
		finish_npcId = finishNpcId;
	}

	public List<MmochatTaskTalk> getAccept_talk() {
		return accept_talk;
	}

	public static MmochatTaskTalk convertTaskChat(MmochatTaskTalk talk,
			MmochatPlayer p) {
		if (talk.getMsg().startsWith("[roleName]")) {
			talk.setBmpType(p.getBmpType());
		}
		String newTalk = talk.getMsg().replace("[roleName]", p.getName());
		if (p.hasGang()) {
			MmochatGang gang = MmochatGangService.getGangById(p.getGangId());
			if (gang != null) {
				newTalk = newTalk.replace("[帮派名]", gang.getName());
			}
		}
		talk.setMsg(newTalk);
		return talk;
	}

	public MmochatTaskTalk getCurAcceptTalk(MmochatPlayer p) {
		if (cur_accept_talk_index >= accept_talk.size()) {
			cur_accept_talk_index = 0;
		}
		if (accept_talk.size() == 0) {
			return null;
		} else {
			try {
				MmochatTaskTalk talk = accept_talk.get(cur_accept_talk_index);
				MmochatTaskTalk newTalk = convertTaskChat(talk, p);
				return newTalk;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
	}

	public void setAccept_talk(List<MmochatTaskTalk> acceptTalk) {
		accept_talk = acceptTalk;
	}

	public List<MmochatTaskTalk> getFinish_talk() {
		return finish_talk;
	}

	public MmochatTaskTalk getCurFinishTalk(MmochatPlayer p) {
		if (cur_finish_talk_index >= finish_talk.size()) {
			cur_finish_talk_index = 0;
		}
		if (finish_talk.size() == 0) {
			return null;
		} else {
			try {
				MmochatTaskTalk talk = finish_talk.get(cur_finish_talk_index);
				MmochatTaskTalk newTalk = convertTaskChat(talk, p);

				return newTalk;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
	}

	public void setFinish_talk(List<MmochatTaskTalk> finishTalk) {
		finish_talk = finishTalk;
	}

	public void addAccept_talk(String msg) {
		MmochatTaskTalk talk = new MmochatTaskTalk();
		talk.setMsg(msg);
		int offset = msg.indexOf("：");
		if (offset == -1) {
			offset = msg.indexOf(":");
		}
		if (offset != -1) {
			String name = msg.substring(0, offset);
			if (name.equals("[roleName]") || name.equals("帮派总管")) {
				accept_talk.add(talk);
				return;
			}
			for (MmochatNpc npc : MmochatNpcService.npcs.values()) {
				if (npc.getName().equals(name)) {
					talk.setBmpType(npc.getBmpType());
					accept_talk.add(talk);
					return;
				}
			}
			throw new RuntimeException("找不到接任务对话中的NPC,npc name = " + name
					+ ",task name=" + taskName + ",task id=" + taskId);
		} else {
			accept_talk.add(talk);
		}
	}

	public void addFinish_talk(String msg) {
		MmochatTaskTalk talk = new MmochatTaskTalk();
		talk.setMsg(msg);
		int offset = msg.indexOf("：");
		if (offset == -1) {
			offset = msg.indexOf(":");
		}
		if (offset != -1) {
			String name = msg.substring(0, offset);
			if (name.equals("[roleName]") || name.equals("帮派总管")
					|| name.equals("小提示")) {
				finish_talk.add(talk);
				return;
			}
			for (MmochatNpc npc : MmochatNpcService.npcs.values()) {
				if (npc.getName().equals(name)) {
					talk.setBmpType(npc.getBmpType());
					finish_talk.add(talk);
					return;
				}
			}
			throw new RuntimeException("找不到交任务对话中的NPC,npc name = " + name
					+ ",task name=" + taskName + ",task id=" + taskId);
		} else {
			finish_talk.add(talk);
		}
	}

	public int getFinish_award_exp() {
		return finish_award_exp;
	}

	public void setFinish_award_exp(int finishAwardExp) {
		finish_award_exp = finishAwardExp;
	}

	public int getCur_accept_talk_index() {
		return cur_accept_talk_index;
	}

	public void setCur_accept_talk_index(int curAcceptTalkIndex) {
		cur_accept_talk_index = curAcceptTalkIndex;
	}

	public int getCur_finish_talk_index() {
		return cur_finish_talk_index;
	}

	public void setCur_finish_talk_index(int curFinishTalkIndex) {
		cur_finish_talk_index = curFinishTalkIndex;
	}

	public void addCur_accept_talk_index() {
		cur_accept_talk_index++;
		if (cur_accept_talk_index > accept_talk.size()) {
			cur_accept_talk_index = accept_talk.size();
		}
	}

	public void addCur_finish_talk_index() {
		cur_finish_talk_index++;
		if (cur_finish_talk_index > finish_talk.size()) {
			cur_finish_talk_index = finish_talk.size();
		}
	}

	public boolean isAccept_talk_over() {
		return accept_talk.size() == 0
				|| cur_accept_talk_index >= accept_talk.size();
	}

	public boolean isFinish_talk_over() {
		return finish_talk.size() == 0
				|| cur_finish_talk_index >= finish_talk.size();
	}

	public List<MmochatKillMonster> getKillList() {
		return killList;
	}

	public void setKillList(List<MmochatKillMonster> killList) {
		this.killList = killList;
	}

	public void addKillTask(MmochatKillMonster killTask) {
		killList.add(killTask);
	}

	public String getTaskDetail() {
		return taskDetail;
	}

	public void setTaskDetail(String taskDetail) {
		this.taskDetail = taskDetail;
	}

	public MmochatTaskType getTaskType() {
		return taskType;
	}

	public void setTaskType(MmochatTaskType taskType) {
		this.taskType = taskType;
	}

	public long getFinish_time() {
		return finish_time;
	}

	public void setFinish_time(long finishTime) {
		finish_time = finishTime;
	}

	public Integer getNext_taskId() {
		return next_taskId;
	}

	public void setNext_taskId(Integer nextTaskId) {
		next_taskId = nextTaskId;
	}

	public String getNext_taskHintName() {
		return next_taskHintName;
	}

	public void setNext_taskHintName(String nextTaskHintName) {
		next_taskHintName = nextTaskHintName;
	}

	public String getNext_taskHint() {
		return next_taskHint;
	}

	public void setNext_taskHint(String nextTaskHint) {
		next_taskHint = nextTaskHint;
	}

	public Integer getNextAccept_taskId() {
		return nextAccept_taskId;
	}

	public void setNextAccept_taskId(Integer nextAcceptTaskId) {
		nextAccept_taskId = nextAcceptTaskId;
	}

	public List<MmochatMaterial> getFinish_need_materials() {
		return finish_need_materials;
	}

	public void setFinish_need_materials(
			List<MmochatMaterial> finishNeedMaterials) {
		finish_need_materials = finishNeedMaterials;
	}

	public List<MmochatDrug> getFinish_need_drugs() {
		return finish_need_drugs;
	}

	public void setFinish_need_drugs(List<MmochatDrug> finishNeedDrugs) {
		finish_need_drugs = finishNeedDrugs;
	}

	public int getFinish_need_money() {
		return finish_need_money;
	}

	public void setFinish_need_money(int finishNeedMoney) {
		finish_need_money = finishNeedMoney;
	}

	public void addFinish_need_material(MmochatMaterial material) {
		finish_need_materials.add(material);
	}

	public void addFinish_need_drug(MmochatDrug drug) {
		finish_need_drugs.add(drug);
	}

	public List<MmochatMaterial> getFinish_award_materials() {
		return finish_award_materials;
	}

	public void setFinish_award_materials(
			List<MmochatMaterial> finishAwardMaterials) {
		finish_award_materials = finishAwardMaterials;
	}

	public List<MmochatDrug> getFinish_award_drugs() {
		return finish_award_drugs;
	}

	public void setFinish_award_drugs(List<MmochatDrug> finishAwardDrugs) {
		finish_award_drugs = finishAwardDrugs;
	}

	public int getFinish_award_money() {
		return finish_award_money;
	}

	public void setFinish_award_money(int finishAwardMoney) {
		finish_award_money = finishAwardMoney;
	}

	public void addFinish_award_material(MmochatMaterial material) {
		finish_award_materials.add(material);
	}

	public void addFinish_award_drug(MmochatDrug drug) {
		finish_award_drugs.add(drug);
	}

	public MmochatNpcBattle getFinish_need_battles() {
		return finish_need_battles;
	}

	public void setFinish_need_battle(MmochatNpcBattle finishNeedBattles) {
		finish_need_battles = finishNeedBattles;
	}

	public boolean needBattleBeforeFinish() {
		return finish_need_battles != null;
	}

	public int getAccept_maxLevel() {
		return accept_maxLevel;
	}

	public void setAccept_maxLevel(int acceptMaxLevel) {
		accept_maxLevel = acceptMaxLevel;
	}

	public void setFinish_need_battles(MmochatNpcBattle finishNeedBattles) {
		finish_need_battles = finishNeedBattles;
	}

	public long getAccept_time() {
		return accept_time;
	}

	public void setAccept_time(long acceptTime) {
		accept_time = acceptTime;
	}

	public boolean isTaskContinue() {
		return taskContinue;
	}

	public void setTaskContinue(boolean taskContinue) {
		this.taskContinue = taskContinue;
	}

	public int getFinishAnotherTaskId() {
		return finishAnotherTaskId;
	}

	public void setFinishAnotherTaskId(int finishAnotherTaskId) {
		this.finishAnotherTaskId = finishAnotherTaskId;
	}

	public int getTaskTurn() {
		return taskTurn;
	}

	public void setTaskTurn(int taskTurn) {
		this.taskTurn = taskTurn;
	}

	public void addTaskTurn() {
		taskTurn++;
		if (taskTurn > taskMaxTurn) {
			taskTurn = 1;
		}
	}

	public int getTaskMaxTurn() {
		return taskMaxTurn;
	}

	public void setTaskMaxTurn(int taskMaxTurn) {
		this.taskMaxTurn = taskMaxTurn;
	}

	public List<MmochatEquipAward> getFinish_award_equips() {
		return finish_award_equips;
	}

	public void setFinish_award_equips(List<MmochatEquipAward> finishAwardEquips) {
		finish_award_equips = finishAwardEquips;
	}

	public List<MmochatMonster> getFinish_award_pet() {
		return finish_award_pet;
	}

	public void setFinish_award_pet(List<MmochatMonster> finishAwardPet) {
		finish_award_pet = finishAwardPet;
	}

	// 任务是否已过期
	public boolean isInvalid() {
		if (taskLife > 0) {
			if (System.currentTimeMillis() - accept_time > taskLife) {
				// 过期
				return true;
			}
		}
		if (taskType.isHasFinishNpc()) {
			int npcId = getFinish_npcId();
			MmochatNpc npc = MmochatNpcService.npcs.get(npcId);
			if (npc == null) {
				// 过期
				return true;
			}
		}
		// 未过期
		return false;
	}

	public long getTaskLife() {
		return taskLife;
	}

	public void setTaskLife(long taskLife) {
		this.taskLife = taskLife;
	}

	public int getFinish_award_dao() {
		return finish_award_dao;
	}

	public void setFinish_award_dao(int finishAwardDao) {
		finish_award_dao = finishAwardDao;
	}

	public List<MmochatPropType> getFinish_award_prop() {
		return finish_award_prop;
	}

	public void setFinish_award_prop(List<MmochatPropType> finishAwardProp) {
		finish_award_prop = finishAwardProp;
	}

	public int getFinish_award_gangBuild() {
		return finish_award_gangBuild;
	}

	public void setFinish_award_gangBuild(int finishAwardGangBuild) {
		finish_award_gangBuild = finishAwardGangBuild;
	}

	public boolean isCanReject() {
		return canReject;
	}

	public void setCanReject(boolean canReject) {
		this.canReject = canReject;
	}

	public int getFinish_award_gangMoney() {
		return finish_award_gangMoney;
	}

	public void setFinish_award_gangMoney(int finishAwardGangMoney) {
		finish_award_gangMoney = finishAwardGangMoney;
	}

	public boolean isShowInTaskList() {
		return showInTaskList;
	}

	public void setShowInTaskList(boolean showInTaskList) {
		this.showInTaskList = showInTaskList;
	}

	public Integer getHintKey() {
		return hintKey;
	}

	public void setHintKey(Integer hintKey) {
		this.hintKey = hintKey;
	}

	public String getHintWord() {
		return hintWord;
	}

	public void setHintWord(String hintWord) {
		this.hintWord = hintWord;
	}

	public int getTargetMapId() {
		return targetMapId;
	}

	public void setTargetMapId(int targetMapId) {
		this.targetMapId = targetMapId;
	}

	public int getTargetX() {
		return targetX;
	}

	public void setTargetX(int targetX) {
		this.targetX = targetX;
	}

	public int getTargetY() {
		return targetY;
	}

	public void setTargetY(int targetY) {
		this.targetY = targetY;
	}

	public int getFinish_need_zhanji() {
		return finish_need_zhanji;
	}

	public void setFinish_need_zhanji(int finishNeedZhanji) {
		finish_need_zhanji = finishNeedZhanji;
	}

	public Integer getGuide_next_taskNpcId() {
		return guide_next_taskNpcId;
	}

	public void setGuide_next_taskNpcId(Integer guideNextTaskNpcId) {
		guide_next_taskNpcId = guideNextTaskNpcId;
	}

	public int getFinish_award_expTimes() {
		return finish_award_expTimes;
	}

	public void setFinish_award_expTimes(int finishAwardExpTimes) {
		finish_award_expTimes = finishAwardExpTimes;
	}

	// 返回一个以此任务作为前置任务的任务
	public MmochatTask getTaskAfterThis() {
		for (MmochatTask t : MmochatTaskService.tasks.values()) {
			for (Integer theTaskId : t.getAccept_preTaskIds()) {
				if (taskId == theTaskId) {
					return t;
				}
			}
		}
		return null;
	}

	public int getTaskLevel() {
		return taskLevel;
	}

	public void setTaskLevel(int taskLevel) {
		this.taskLevel = taskLevel;
	}

	public int getTaskTag() {
		return taskTag;
	}

	public void setTaskTag(int taskTag) {
		this.taskTag = taskTag;
	}

}
